package com.fysports.web.controller.system;

import com.jfinal.json.Json;
import com.jfinal.plugin.activerecord.Page;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.controller.engine.RestFulController;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.kit.dtgrid.DTGridKit;
import com.fysports.web.kit.dtgrid.DTGridView;
import com.fysports.web.model.system.SystemRole;
import com.fysports.web.model.system.SystemUser;
import com.fysports.web.model.system.SystemUserRole;
import com.fysports.web.model.user.BizUser;
import com.fysports.web.module.core.DefaultModuleBuilder;
import com.fysports.web.module.core.IModuleFactory;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;

/**
 * 系统用户角色管理
 * Created by sunk
 * Date : 15-11-27 下午11:30
 * Project : JdmData_WebServer
 */
public class ManagerUserRoleController extends AbstractBaseController implements RestFulController{
    IModuleFactory imf = new DefaultModuleBuilder();

    public void index() {
        imf.create(this);
        execute();
        render(PathBizKit.getPageAdminPath("system/role/userrole/userrole_list.ftl"));
    }

    public void load() {
        DTGridView dtgrid = DTGridKit.toDtView(getPara(DTGridKit.PARAM_NAME));
        String sql = "select sru.*,sr.rolename,su.`name`";
        String sqlExceptSelect = "from sys_role_user sru inner join sys_role sr on (sru.roleuuid = sr.uuid) inner join sys_user su on (sru.useruuid = su.uuid) where sru.liveflag=? and sr.liveflag=? and su.liveflag=?";
        Page<SystemUserRole> list = SystemUserRole.dao.paginate(dtgrid.getNowPage(), dtgrid.getPageSize(), sql, sqlExceptSelect,"1","1","1");
        dtgrid = DTGridKit.toDtView(dtgrid,list);
        renderJson(Json.getJson().toJson(dtgrid));
    }
    @Override
    public void delete() {
        String uuid = getPara();
        JsonView json = new JsonView();
        SystemUserRole su = SystemUserRole.dao.findById(uuid);
        boolean status = false;
        if (su != null) {
            su.set(SystemUserRole.COL_LIVEFLAG, "0");
            status = su.update();
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }
    @Override
    public void add() {
        setAttr("users", SystemUser.dao.search(BizUser.COL_LIVEFLAG, "1"));
        setAttr("roles", SystemRole.dao.search(SystemRole.COL_LIVEFLAG, "1"));
        setAttr("m", new SystemUserRole());
        render(PathBizKit.getPageAdminPath("system/role/userrole/userrole_add.ftl"));
    }
    @Override
    public void save() {
        SystemUserRole sur = getModel(SystemUserRole.class, "m");
        boolean status = sur.save();
        renderJson(new JsonView().setStatus(status).setMsg(status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL));
    }
    @Override
    public void edit() {
        String uuid = getPara();
        setAttr("users", SystemUser.dao.search(BizUser.COL_LIVEFLAG, "1"));
        setAttr("roles", SystemRole.dao.search(SystemRole.COL_LIVEFLAG, "1"));
        setAttr("m", SystemUserRole.dao.findById(uuid));
        render(PathBizKit.getPageAdminPath("system/role/userrole/userrole_update.ftl"));
    }
    @Override
    public void update() {
        SystemUserRole sur = getModel(SystemUserRole.class, "m");
        boolean status = sur.update();
        renderJson(new JsonView().setStatus(status).setMsg(status ? OperateType.UPDATE_SUCCESS : OperateType.SAVE_FAIL));
    }
}
